var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    winHeight: 510,//获取手机屏幕的高度
    btnShareIsOk: 0,
    btnShareImageOk: 0,
    enve_id:'',//红包ID
    head_img:'',//分享用户头像
    uid:'',//用户id
    codeUri:'',//分享到朋友圈的二维码图片
    shareTitle:'',//分享标题
    tips:'',
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    console.log(options);
    _this.setData({
        enve_id: options.enve_id,
        uid: options.uid,
      });
    _this.loop();
  },
  // 判断是否获取到token的值
  loop: function () {
    var _this = this;
    var theToken = wx.getStorageSync("token");
    if (!theToken) {
      setTimeout(function () { _this.loop() }, 100);
    } else {
      console.log(theToken);
      _this.setData({
        token: theToken,
      });
      _this.shareInitConfig();
      _this.shareInfo();
    }
  },
  /*加载分享专属配置*/
  shareInitConfig: function () {
    var _this = this;
    var requestData = {
      token: _this.data.token,
      version: app.setConfig.version
    }
    app.post('/Enve/getShareConfig', requestData, function (res) {
      if (res.code == 2000) {
        _this.setData({
          head_img: res.data.head_img,
          tips: res.data.tips
        });
      }else{
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 2000
        });
      }
    });
  },
  /*分享获取数据*/
  shareInfo:function(){
    var _this = this;
    var requestData = {
      token: _this.data.token,
      version: app.setConfig.version,
      enve_id:_this.data.enve_id,
      uid: _this.data.uid,
      head_img: _this.data.head_img,
      page:'pages/red-packet/red-packet'
    };
    app.post('/api/v1/Enve/shareInfo', requestData, function (res) {
           if (res.code == 2000){
              console.log()
              _this.setData({
                codeUri: res.codeImage,
                shareTitle: res.data.enve_share_title
              });
          } else if (!res.code){
            wx.showToast({
              title: '服务器去月球了',
              duration: 2000
            })
          }else{
             wx.showToast({
               title: res.msg,
               icon: "none",
               duration: 2000
             });
          }
         _this.setData({
           btnShareImageOk:1,
           btnShareIsOk:1
         });
    });
  },
   /** 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var _this = this;
    var shareTitle = _this.data.shareTitle;
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: shareTitle,
      path: '/pages/red-packet/red-packet?enve_id=' + _this.data.enve_id+ '&uid=' + _this.data.uid,
      success: function (res) {
        // 转发成功
         wx.navigateTo({
           url: '../withdrawals/withdrawals?enve_id=' + _this.data.enve_id,
         })
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  /*查看分享图片*/
  getShareImg:function(){
    var _this = this;
    var theShareImg = _this.data.codeUri;
    wx.previewImage({
      current: '', // 当前显示图片的http链接
      urls: [theShareImg] // 需要预览的图片http链接列表
    })
  }
})